const db = require('../db/index');
const moment = require('moment');

exports.addSchedule = (req, res) => {
  const { title, startTime, endTime, description } = req.body;
  const sql = 'INSERT INTO schedules (user_id, title, start_time, end_time, description) VALUES (?, ?, ?, ?, ?)';
  db.query(sql, [req.user.id, title, moment(startTime).format('YYYY-MM-DD HH:mm:ss'), moment(endTime).format('YYYY-MM-DD HH:mm:ss'), description], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc('添加日程安排失败');
    res.cc('添加日程安排成功', 0);
  });
};

exports.getSchedules = (req, res) => {
  const sql = 'SELECT * FROM schedules WHERE user_id = ?';
  db.query(sql, [req.user.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.length < 1) return res.cc('获取日程安排失败');
    res.send({
      status: 0,
      message: '获取日程安排成功',
      data: results
    });
  });
};